package ypmits.components.objectdivider.divider {
	import ypmits.components.util.GrabThumbRoughner;
	import ypmits.setup.EasySetup;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/**
	 * @author Dave Lenz
	 */
	public class HDivider extends Sprite {

		public var prev : HDivider;
		public var next : HDivider;
		
		public var maxy : Number;
		public var index : uint;

		
		
		public function HDivider( width : Number, height : Number )
		{
			draw( width, height );
		}

		
		
		private function draw( width : Number, height : Number ) : void 
		{
			graphics.beginFill( 0x555555 );
			graphics.drawRect( 0, 0, width, height );
			graphics.endFill();
			
			graphics.beginFill( 0x777777 );
			graphics.drawRect( 0, 0, width, 1 );
			graphics.endFill();
			
			graphics.beginFill( 0x222222 );
			graphics.drawRect( 0, height - 1, width, 1 );
			graphics.endFill();
			
			var thumb : GrabThumbRoughner = new GrabThumbRoughner( 11, 4, 0xFFFFFF, true );
			thumb.x = (width * .5) - (thumb.width * .5);
			thumb.y = 2;
			thumb.alpha = .5;
			addChild( thumb );
			
			buttonMode = true;
			
			addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
		}


		
		
		private function mouseDownHandler( e : MouseEvent ) : void 
		{
			removeEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
			var bounds : Rectangle = new Rectangle( );
			bounds.top = prev ? (prev.y + 6) : 0;
			bounds.bottom = next ? (next.y - 6) : maxy;
			EasySetup.instance.stage.addEventListener( MouseEvent.MOUSE_UP, mouseUpHandler );
			startDrag( false, bounds );
			dispatchEvent( new Event( Event.CHANGE ) );
		}

		
		
		private function mouseUpHandler( e : MouseEvent ) : void 
		{
			stopDrag();
			addEventListener( MouseEvent.MOUSE_DOWN, mouseDownHandler );
			EasySetup.instance.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler );
			dispatchEvent( new Event( Event.COMPLETE ) );
		}
	}
}
